/**
 * 学校管理的单例
 */
var School = {
    id: "schoolTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
School.initColumn = function () {
    var columns = [
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '学校代号', field: 'schoolCode', align: 'center', valign: 'middle', sortable: true},
        {title: '学校名称', field: 'schoolName', align: 'center', valign: 'middle', sortable: true}]
    return columns;
};


/**
 * 检查是否选中
 */
School.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
    	School.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加学校
 */
School.openAddSchool = function () {
    var index = layer.open({
        type: 2,
        title: '添加学校',
        area: ['400px', '400px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/seniorschool/edit'
    });
    this.layerIndex = index;
};

/**
 * 点击修改按钮时
 */
School.openChangeSchool = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '修改学校',
            area: ['400px', '400px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/seniorschool/edit?id=' + School.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除学校
 */
School.delSchool = function () {
    if (this.check()) {
        var operation = function(){
            var ajax = new $ax(Feng.ctxPath + "/seniorschool/remove", function () {
                Feng.success("删除成功!");
                School.table.refresh();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("schoolId", School.seItem.id);
            ajax.start();
        };

        Feng.confirm("是否删除学校 " + School.seItem.schoolName + "？",operation);
    }
};


School.resetSearch = function () {
    $("#schoolCode").val("");
    $("#schoolName").val("");
    School.search();
}



/**
 * 搜索学校
 */
School.search = function () {
    var queryData = {};
    queryData['schoolCode'] = $("#schoolCode").val();
    queryData['schoolName'] = $("#schoolName").val();
    School.table.refresh({query: queryData});
}

$(function () {
    var defaultColunms = School.initColumn();
    var table = new BSTable(School.id, "/seniorschool/list", defaultColunms);
    table.init();
    School.table = table;
});
